﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NLog;
using OSA.Policlinic.UI.WFApplicationWCF.RegistryServiceReference;

namespace OSA.Policlinic.UI.WFApplication
{
    public partial class TicketControl : UserControl
    {
        private MedService medService;
        public Patient patient;
        public Doctor doctor;
        private IRegistryBusinessApplication registryBusinessApplication;
        private static Logger logger = LogManager.GetCurrentClassLogger();

        public TicketControl()
        {
            InitializeComponent();
            registryBusinessApplication = RegistryBusinessApplicationHome.GetInstanse();
            logger.Trace("Контрол TicketControl успешно инициализирован конструктором по умолчанию");
        }

        public TicketControl(Doctor doc)
        {
            InitializeComponent();
            registryBusinessApplication = RegistryBusinessApplicationHome.GetInstanse();
            logger.Trace("Контрол TicketControl успешно инициализирован, выбран врач id = {0}", doc.Id);
            doctor = doc;
            FillDoctor();
        }

        public TicketControl(Patient p)
        {
            InitializeComponent();
            registryBusinessApplication = RegistryBusinessApplicationHome.GetInstanse();
            logger.Trace("Контрол TicketControl успешно инициализирован, выбран пациент id = {0}", p.Id);
            patient = p;
            FillPatient();
        }

        private void SelectMedServiceButton_Click(object sender, EventArgs e)
        {
            ChooseServiseForm chooseServiseForm = new ChooseServiseForm();
            logger.Debug("Нажата кнопка 'Выбрать услугу' в контроле талона");
            chooseServiseForm.ShowDialog();
            medService = chooseServiseForm.choosedMedService;
            FillMefService();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            chooseDoctorForm chooseDoctorForm = new chooseDoctorForm();
            logger.Debug("Нажата кнопка 'Выбрать доктора' в контроле талона");
            chooseDoctorForm.ShowDialog();
            doctor = chooseDoctorForm.ChoosedDoctor;
            FillDoctor();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            choosePatientForm choosePatientForm = new choosePatientForm();
            logger.Debug("Нажата кнопка 'Выбрать пациента' в контроле талона");
            choosePatientForm.ShowDialog();
            patient = choosePatientForm.ChoosedPatient;
            FillPatient();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            logger.Debug("Нажата кнопка 'Сохранить' в контроле талона");
            if(patient == null)
            {
                logger.Debug("Не выбран пациент при попытке сохранить новый талон");
                MessageBox.Show("Не выбран пациент");
                button2_Click(sender, e);
                return;
            }
            if (doctor == null)
            {
                logger.Debug("Не выбран врач при попытке сохранить новый талон");
                MessageBox.Show("Не выбран доктор");
                button1_Click(sender, e);
                return;
            }
            if (medService == null)
            {
                logger.Debug("Не выбрана услуга при попытке сохранить новый талон");
                MessageBox.Show("Не выбрана услуга");
                SelectMedServiceButton_Click(sender, e);
                return;
            }
            if (patient != null && doctor != null && medService != null && textBox13 != null)
            {
                Ticket newTicket = new Ticket(dateTimePicker1.Value,
                                              textBox13.Text, doctor, patient, medService);
                registryBusinessApplication.SaveTicket(newTicket);
                logger.Debug("Новый талон успешно сохранен");
                MessageBox.Show("Талон добавлен");
            }
        }

        public void ClearDoctor()
        {
            doctor = null;
            textBox9.Text = null;
            textBox10.Text = null;
            textBox11.Text = null;
            textBox12.Text = null;
        }

        public void ClearPatient()
        {
            patient = null;
            textBox3.Text = null;
            textBox4.Text = null;
            textBox5.Text = null;
            textBox6.Text = null;
            textBox7.Text = null;
            textBox8.Text = null;
        }

        public void ClearService()
        {
            medService = null;
            textBox1.Text = null;
            textBox2.Text = null;
            textBox13.Text = null;
            dateTimePicker1.Value = DateTime.Now;
        }

        public void FillDoctor()
        {
            if (doctor != null)
            {
                textBox9.Text = doctor.Employee.ToString();
                textBox10.Text = doctor.Specialisation;
                textBox11.Text = doctor.Category;
                textBox12.Text = doctor.Cabinet.ToString();
            }
        }

        public void FillPatient()
        {
            if (patient != null)
            {
                textBox3.Text = patient.Surname + " " + patient.Name + " " + " " + patient.Middlename;
                textBox4.Text = string.Format("{0}.{1}.{2}", patient.Birth.Day, patient.Birth.Month, patient.Birth.Year);
                textBox6.Text = patient.Address.ToString();
                textBox5.Text = patient.InsurancePolis.InsuranceOrganization.Name;
                textBox7.Text = patient.InsurancePolis.Series;
                textBox8.Text = patient.InsurancePolis.PolisNumber;
                textBox14.Text = string.Format("{0}.{1}.{2}", patient.InsurancePolis.Registration.Day, patient.InsurancePolis.Registration.Month, patient.InsurancePolis.Registration.Year);
                textBox15.Text = string.Format("{0}.{1}.{2}", patient.InsurancePolis.Expiration.Day, patient.InsurancePolis.Expiration.Month, patient.InsurancePolis.Expiration.Year);
            }
        }

        public void FillMefService()
        {
            if (medService != null)
            {
                textBox1.Text = medService.Name;
                textBox2.Text = medService.Price.ToString();
            }
        }
    }
}
